//
//  RegisterView.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/11.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "RegisterView.h"
#import "RegisterHeader.h"

@interface RegisterView()

@property (nonatomic, strong) UIButton *clearBtn;

// 存值
@property (nonatomic, copy) NSString *phoneNumStr;
@property (nonatomic, copy) NSString *codeStr;
@property (nonatomic, copy) NSString *pwdStr;


@end


@implementation RegisterView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self loadRegisterSubviews];
    }
    return self;
}


// loadSubviews
- (void)loadRegisterSubviews{
    
    // 电话
    UIView *phoneNumView = [[UIView alloc]init];
    phoneNumView.backgroundColor = [UIColor whiteColor];
    [self addSubview:phoneNumView];
    [phoneNumView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(CellFirstTop);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(CellViewH);
    }];
    
    UIImageView *phoneImgView = [[UIImageView alloc]init];
    phoneImgView.image = [UIImage imageNamed:@"login_icon_shouji"];
    phoneImgView.contentMode = UIViewContentModeScaleAspectFit;
    [phoneNumView addSubview:phoneImgView];
    [phoneImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumView).offset(23*HEIGHT);
        make.left.equalTo(phoneNumView).offset(CellImgLeft);
    }];
    
    UITextField *phoneTF = [[UITextField alloc]init];
    phoneTF.placeholder = RegisterPhonePlace;
    phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    phoneTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    phoneTF.textColor = KLColor_B2;
    phoneTF.textAlignment = NSTextAlignmentLeft;
    [phoneNumView addSubview:phoneTF];
    [[phoneTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(UITextField *phone) {
        self.phoneNumStr = phone.text;
        if (self.phoneNumStr.length == 11) {
            [self.codeButton initialTime];
        }
    }];
    [phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgView);
        make.width.mas_equalTo(200*WIDTH);
        make.left.equalTo(phoneImgView.mas_right).offset(CellTextfieldLeft);
    }];
    
    UIView *lineView_phone = [[UIView alloc]init];
    lineView_phone.backgroundColor = KLColor_B3;
    [phoneNumView addSubview:lineView_phone];
    [lineView_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgView);
        make.right.equalTo(phoneNumView).offset(-VerticalLineRight);
        make.width.mas_equalTo(LineOne);
        make.height.mas_equalTo(LineH);
    }];
    
    SendCodeButton *codeBtn = [SendCodeButton buttonWithType:UIButtonTypeCustom];
    [codeBtn setTitle:ResgiterGetCode forState:UIControlStateNormal];
    [codeBtn setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    codeBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
    [phoneNumView addSubview:codeBtn];
    self.codeButton = codeBtn;
   
    [[codeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(SendCodeButton *x) {
        
        
        if (self.delegateCode) {
            NSString *phoneStr = self.phoneNumStr.length > 0 ? self.phoneNumStr:@"";
            [self.delegateCode sendNext:@{@"phoneStr":phoneStr}];
        }
    }];
    
    
    [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgView);
        make.centerX.equalTo(phoneNumView.mas_right).offset(-60*WIDTH);
    }];
    
    UIView *bottomLine_phone = [[UIView alloc]init];
    bottomLine_phone.backgroundColor = KLColor_B3;
    [phoneNumView addSubview:bottomLine_phone];
    [bottomLine_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(phoneNumView);
        make.width.mas_equalTo(BottoLineW);
        make.height.mas_equalTo(LineOne);
    }];
    
    
    // 验证码
    UIView *codeView = [[UIView alloc]init];
    codeView.backgroundColor = [UIColor whiteColor];
    [self addSubview:codeView];
    [codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(CellViewH);
    }];
    
    UIImageView *codeImgView = [[UIImageView alloc]init];
    codeImgView.image = [UIImage imageNamed:@"login_icon_yanzhengma"];
    codeImgView.contentMode = UIViewContentModeScaleAspectFit;
    [codeView addSubview:codeImgView];
    [codeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeView).offset(23*HEIGHT);
        make.left.equalTo(codeView).offset(CellImgLeft);
    }];
    
    UITextField *codeTF = [[UITextField alloc]init];
    codeTF.placeholder = RegisterCodePlace;
    codeTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    codeTF.textColor = KLColor_B2;
    codeTF.keyboardType = UIKeyboardTypeNumberPad;
    codeTF.textAlignment = NSTextAlignmentLeft;
    [codeView addSubview:codeTF];
    [[codeTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(UITextField *code) {
        self.codeStr = code.text;
    }];
    [codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(codeImgView);
        make.width.mas_equalTo(200*WIDTH);
        make.left.equalTo(codeImgView.mas_right).offset(CellTextfieldLeft);
    }];
    
    UIView *bottomLine_code = [[UIView alloc]init];
    bottomLine_code.backgroundColor = KLColor_B3;
    [codeView addSubview:bottomLine_code];
    [bottomLine_code mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(codeView);
        make.width.mas_equalTo(BottoLineW);
        make.height.mas_equalTo(LineOne);
    }];
    // 密码
    
    UIView *pwdView = [[UIView alloc]init];
    pwdView.backgroundColor = [UIColor whiteColor];
    [self addSubview:pwdView];
    [pwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(CellViewH);
    }];

    UIImageView *pwdImgView = [[UIImageView alloc]init];
    pwdImgView.image = [UIImage imageNamed:@"login_icon_mima"];
    pwdImgView.contentMode = UIViewContentModeScaleAspectFit;
    [pwdView addSubview:pwdImgView];
    [pwdImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(pwdView).offset(23*HEIGHT);
        make.left.equalTo(pwdView).offset(CellImgLeft);
    }];

    UITextField *pwdTF = [[UITextField alloc]init];
    pwdTF.placeholder = RegisterPwdPlace;
    pwdTF.secureTextEntry = YES;
    pwdTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    pwdTF.textColor = KLColor_B2;
    pwdTF.textAlignment = NSTextAlignmentLeft;
    pwdTF.clearButtonMode=UITextFieldViewModeWhileEditing;
    [pwdView addSubview:pwdTF];
    [[pwdTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(UITextField *pwd) {
        self.pwdStr = pwd.text;
        self.clearBtn.hidden = pwd.text.length <= 0;
        if (pwd.text.length < 6 || pwd.text.length > 18) {
            // 加提醒
            
        }
    }];
    [pwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pwdImgView);
        make.right.equalTo(pwdView).offset(-25*WIDTH);
        make.left.equalTo(pwdImgView.mas_right).offset(CellTextfieldLeft);
    }];


    UIView *bottomLine_pwd = [[UIView alloc]init];
    bottomLine_pwd.backgroundColor = KLColor_B3;
    [pwdView addSubview:bottomLine_pwd];
    [bottomLine_pwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(pwdView);
        make.width.mas_equalTo(BottoLineW);
        make.height.mas_equalTo(LineOne);
    }];

    // 注册按钮
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerBtn setTitle:ResgiterBtnTitle forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18*WIDTH];
    [registerBtn setBackgroundImage:[UIImage createImageWithColor:KLColor_A1] forState:UIControlStateNormal];
    [self addSubview:registerBtn];
    self.registerBtn = registerBtn;
    [[registerBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (self.delegateSignal) {
            [self.delegateSignal sendNext:@{@"phoneStr":self.phoneNumStr,@"codeStr":self.codeStr,@"pwd":self.pwdStr}];
        }
    }];
    
    RAC(registerBtn,enabled) = [RACSignal combineLatest:@[RACObserve(self, phoneNumStr),RACObserve(self, codeStr),RACObserve(self, pwdStr)] reduce:^id(NSString *phoneStr,NSString*codeStr,NSString*pwdStr){
        BOOL enable;
        enable = phoneStr.length>0&&codeStr.length>0&&pwdStr.length>0;
        return @(enable);
    }];
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(pwdView.mas_bottom).offset(RegisterBtnTop);
        make.width.mas_equalTo(RegisterBtnW);
        make.height.mas_equalTo(RegisterBtnH);
    }];
    
    // 协议
    UILabel *tintLb = [[UILabel alloc]init];
    tintLb.text = ResgiterProtocolTint;
    tintLb.textColor = KLColor_B4;
    tintLb.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12*WIDTH];
    [self addSubview:tintLb];
    [tintLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(registerBtn.mas_bottom).offset(RegisterProTop);
        make.left.equalTo(self).offset(RegisterProMargin);
    }];
    
    UIButton *protocolBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [protocolBtn setTitle:ResgiterProtocol forState:UIControlStateNormal];
    [protocolBtn setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    protocolBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12*WIDTH];
    self.protocolBtn = protocolBtn;
    [self addSubview:protocolBtn];
    [protocolBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tintLb);
        make.right.equalTo(self).offset(-RegisterProMargin);
    }];
    
}


// 圆角
- (void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    [self.registerBtn layoutIfNeeded];
    [self.protocolBtn layoutIfNeeded];
    [self.registerBtn setRadiusWithValue:22*HEIGHT];
    [self.protocolBtn addBottomLineWithColor:KLColor_A1 WithHeight:20*HEIGHT];
}



@end
